package com.iss.card.view.store;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.iss.card.R;
import com.iss.card.view.model.CityBean;
/**
 * search info by text in eidt text.
 */
public class CityListAdapter extends BaseAdapter{

	List<CityBean> mCityInfo ;
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	public CityListAdapter(Context context, List<CityBean> al) {
		mLayoutInflater = LayoutInflater.from(context);
		this.mCityInfo = al;
		mContext = context;
	}

	@Override
	public int getCount() {
		if(mCityInfo != null){
			return mCityInfo.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.citylist_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.cityname);
			holder.count = (TextView) convertView.findViewById(R.id.storescount);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		CityBean city = mCityInfo.get(position);
		holder.name.setText(getCityName(position));
		holder.count.setText(String.format(
				mContext.getResources().getString(
						R.string.city_list_item_store_number),
				city.getStoreNumber()));

		return convertView;
	}
	
	private class ViewHolder {
		private TextView name;
		private TextView count;
	}
	
	private String getCityName(int position) {
		if (mCityInfo != null) {
			return mCityInfo.get(position).getCityName();
		} 
		return "";
	}
	
	private String getStoreCount(int position) {
		if (mCityInfo != null) {
			return mCityInfo.get(position).getStoreNumber();
		} 
		return "";
	}
}
